package main

import (
	"bytes"
	"compress/zlib"
	"encoding/base64"
	"io"
	"io/ioutil"
	"myTest/xxtea"
)

/**
通用测试
 */

//func main() {
//	const (
//		a = 5
//		b = 0
//		c = iota
//	)
//	fmt.Println(a,b,c)
//}


func main() {


	//Encrypt(key)
	//Decrypt()
	//DecryptLua()
}

func Encrypt()  {
	key, errKey := ioutil.ReadFile("/Users/zgh/leetCode/test/key.out")
	if errKey != nil {
		panic(errKey)
	}

	readData, errRe := ioutil.ReadFile("./decrypt.out")
	if errRe != nil {
		panic(errRe)
	}
	head, errRe := ioutil.ReadFile("./head.out")
	if errRe != nil {
		panic(errRe)
	}

	compressData := DoZlibCompress(readData)
	encrypt_data := xxtea.Encrypt(compressData, key)
	for idx := range encrypt_data{
		head = append(head, encrypt_data[idx])
	}
	//decodeByte := []byte(string(head) + string(encrypt_data))

	b64 := base64.StdEncoding
	decodeByte := make([]byte, b64.EncodedLen(len(head)))
	b64.Encode(decodeByte, head)

	errWr := ioutil.WriteFile("/Users/zgh/leetCode/test/encrypt.out", decodeByte, 0666) //写入文件(字节数组)
	if errWr != nil {
		panic(errWr)
	}
}

func Decrypt() {
	//key, errKey := ioutil.ReadFile("/Users/zgh/leetCode/test/key.out")
	//if errKey != nil {
	//	panic(errKey)
	//}

	readData, errRe := ioutil.ReadFile("/Users/zgh/leetCode/test/encrypt.out")
	if errRe != nil {
		panic(errRe)
	}

	b64 := base64.StdEncoding
	decodeStr, errB64 := b64.DecodeString(string(readData))
	if errB64 != nil{
		panic(errB64)
	}

	if decodeStr[0] == '0' && decodeStr[1] == '0' && decodeStr[2] == '0'{
		head := decodeStr[:8]
		errWr := ioutil.WriteFile("./head.out", head, 0666) //写入文件(字节数组)
		if errWr != nil {
			panic(errWr)
		}

		decodeStr = decodeStr[8:];
	}

	//encrypt_data := xxtea.Encrypt(readData, []byte(key))
	//decrypt_data := xxtea.Decrypt(decodeStr, key)
	unComData := DoZlibUnCompress(decodeStr)
	//fmt.Println(string(decrypt_data))
	errWr := ioutil.WriteFile("./decrypt.out", unComData, 0666) //写入文件(字节数组)
	if errWr != nil {
		panic(errWr)
	}
}

func DecryptLua() {
	key, errKey := ioutil.ReadFile("/Users/zgh/leetCode/test/keyLua.out")
	if errKey != nil {
		panic(errKey)
	}

	readData, errRe := ioutil.ReadFile("/Users/zgh/leetCode/test/login2.lua")
	if errRe != nil {
		panic(errRe)
	}

	//b64 := base64.StdEncoding
	//decodeStr, errB64 := b64.DecodeString(string(readData))
	//if errB64 != nil{
	//	panic(errB64)
	//}

	if readData[0] == 'D' && readData[1] == 'A' && readData[2] == 'D' && readData[3] == 'D' && readData[4] == 'Y'{
		//head := readData[:6]
		//errWr := ioutil.WriteFile("./head.out", head, 0666) //写入文件(字节数组)
		//if errWr != nil {
		//	panic(errWr)
		//}

		readData = readData[5:];
	}

	//encrypt_data := xxtea.Encrypt(readData, []byte(key))
	decrypt_data := xxtea.Decrypt(readData, key)
	//unComData := DoZlibUnCompress(decrypt_data)
	//fmt.Println(string(decrypt_data))
	errWr := ioutil.WriteFile("./decryptLua.lua", decrypt_data, 0666) //写入文件(字节数组)
	if errWr != nil {
		panic(errWr)
	}
}

//进行zlib压缩
func DoZlibCompress(src []byte) []byte {
	var in bytes.Buffer
	w := zlib.NewWriter(&in)
	_, err := w.Write(src)
	if err != nil{
		panic(err)
	}

	errClose := w.Close()
	if errClose != nil{
		panic(errClose)
	}

	return in.Bytes()
}

//进行zlib解压缩
func DoZlibUnCompress(compressSrc []byte) []byte {
	b := bytes.NewReader(compressSrc)
	var out bytes.Buffer
	r, _ := zlib.NewReader(b)
	_, err := io.Copy(&out, r)
	if err != nil{
		panic(err)
	}
	return out.Bytes()
}
